/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.1


Rectangle {
    id: screenChannelList
    anchors.fill:parent

    FocusScope {
        id: scope

        property alias color: screenChannelList.color
        x: screenChannelList.x; y: screenChannelList.y
        width: screenChannelList.width; height: screenChannelList.height
    }

    Item {
        id: delegateItem
        objectName: "delegateItem"
        signal epgTriggerVisibleUpdate( int firstVisibleItem, int lastVisibleItem )
        Timer {
            interval: 60000; running: true; repeat: true
            onTriggered: {
                if ( listChannelList.visible && listChannelList.count > 0 ){
                    //Emit the signal to update currently visible items
                    delegateItem.epgTriggerVisibleUpdate( listChannelList.indexAt( 0, listChannelList.contentY ),
                                                          listChannelList.indexAt( 0, listChannelList.contentY + listChannelList.height ) );
                }
            }
        }
    }

    //Display definition of the list entries
    Component {
        id: channelDelegate
        Item {
            id: channelDelegateItem
            width: parent.width; height: delegateTextArea.height + 8 * screenScale
            clip: true

            Row{
                Text {
                    id: idChannelNumber
                    text: channelNumber
                    horizontalAlignment: Text.AlignHCenter
                    font.pixelSize: 18 * screenScale
                    color: "white"
                    width: 50 * screenScale
                    maximumLineCount: 1
                }
                Column {
                    id: delegateTextArea
                    Text {
                        text: channelName
                        color: "white"
                        font.pixelSize: 18 * screenScale
                    }
                    ProgressBar {
                        id: channelProgress
                        anchors.margins: 2 * screenScale
                        style: touchStyleProgressBar
                        width: channelDelegateItem.width - 2 * channelDelegateItem.height
                        value: 1.0 * channelEpgProgress
                    }
                    Grid {
                        columns: 2
                        spacing: 5 * screenScale
                        Text{
                            text: "" + channelEpgTimeFrom
                            color: "white"
                            font.pixelSize: 14 * screenScale
                        }
                        Text {
                            id: idEPG
                            text: "" + channelEpgTitle
                            color: "white"
                            font.pixelSize: 14 * screenScale
                            maximumLineCount: 1
                        }
                        Text{
                            text: "" + channelEpgTimeFromNext
                            color: "white"
                            font.pixelSize: 14 * screenScale
                        }
                        Text {
                            text: "" + channelEpgTitleNext
                            color: "white"
                            font.pixelSize: 14 * screenScale
                            maximumLineCount: 1
                        }
                    }
                }
            }

            MessageDialog {
                id: channelInfoDialog
                visible: false
                title: qsTr( "Program information" )
                Component.onCompleted: visible = false
                onAccepted: {
                    visible = false
                }
            }

            MouseArea {
                id: mouseAreaDelegate
                z: 1
                hoverEnabled: false
                anchors.fill: parent

                onClicked: {
                    console.log("Index clicked: " + index )
                    if ( index >= 0 )
                    {
                        listChannelList.currentIndex = index
                    }
                    focus: true
                }

                onPressAndHold: {
                    console.log("Index long clicked: " + index )
                    if ( index >= 0 )
                    {
                        listChannelList.currentIndex = index
                    }
                    focus: true

                    channelInfoDialog.title = "" + channelNumber + " - " + channelName

                    channelInfoDialog.text = "<b>" + channelEpgTitle + "</b><br>"
                                                   + channelEpgTimeFromTo + "<br>"
                                                   + channelEpgDescription
                    channelInfoDialog.visible = true
                }

            }
        }
    }

    Component {
        id: touchStyleProgressBar
        ProgressBarStyle {
            panel: Rectangle {
                implicitHeight: 4 * screenScale
                color: "#444"
                opacity: 0.8
                Rectangle {
                    antialiasing: true
                    radius: 1 * screenScale
                    color: "#468bb7"
                    height: parent.height
                    width: parent.width * control.value / control.maximumValue
                }
            }
        }
    }

    //Display
    Column {
        id: columnChannelList
        anchors.centerIn: parent
//        spacing: 20 * screenScale

        ListView {
            id: listChannelList
            Rectangle {
                color: "black"
                anchors.top: parent.top
                anchors.topMargin: 0
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                z: -1
            }

            model: ChannelListModel
            delegate: channelDelegate

            width: mainWindow.width
            height: mainWindow.height
            transformOrigin: Item.Center
            focus: true

            onCountChanged: {
                console.log("Listview: Count changed: " + count )
            }
            onVisibleChanged: {
                console.log("Listview: Visible changed: " + visible )
            }
        }
    }
}
